--#include "data\config\boss\WorldBossConfig.lua" once


--[[
是否在活动中
]]
function IsWorldBossValidTime( )
    local now   = System.getCurrMiniTime()
    local dVar  = System.getDyanmicVar()
    if dVar.WorldBossStartTime and dVar.WorldBossStartTime > 0 and now-dVar.WorldBossStartTime >=0 and
        now-dVar.WorldBossStartTime < WorldBossCfg.timer.activityTime + WorldBossCfg.timer.startDelayTime then
        return true
    end

    return false
end

--[[
进入世界BOSS场景
]]
function EnterWorldBoss( sysarg )
    --print("EnterWorldBoss ... ")
    if not IsWorldBossValidTime() then         --不在活动时间内
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0038, ttFlyTip)
        return
    end

    --进入场景
    if CommonEnterScence( sysarg, WorldBossCfg, 0, " ", nil, nil ) then  
		InitWorldBossRightPanel( sysarg )
	end
end

function WorldBossBegin( )
	--boss刷新后设置开始时间
	local dVal = System.getDyanmicVar()
	dVal.WorldBossStartTime = System.getCurrMiniTime()  

	--注册延时刷新
	local NpcPtr = System.getGlobalNpc()
    Actor.unregScriptCallback(NpcPtr, "FreshWorldBoss")
    Actor.regScriptTimer(NpcPtr, 0, WorldBossCfg.timer.startDelayTime * 1000,  0, 1, "FreshWorldBoss")

    Actor.unregScriptCallback(NpcPtr, "WorldBossStop")
    Actor.regScriptTimer(NpcPtr, 0, (WorldBossCfg.timer.activityTime+WorldBossCfg.timer.startDelayTime)*1000, 0, 1, 
        "WorldBossStop")

end


--[[
刷出世界BOSS
]]
function FreshWorldBoss()
	local nDays = System.getDayOfWeek()
	--print("FreshWorldBoss ... weekDay= "..nDays)
	if nDays == 0 then
		nDays = 7
	end

	local dVal = System.getDyanmicVar()
	local bossId = WorldBossCfg.bossId[nDays]
	if bossId then
		--print("FreshWorldBoss bossId= "..bossId)
		FreshCommonBossByBossId(bossId)
        local num = System.getMonsterCountById( bossId, WorldBossCfg.sceneId, 0 )
        if num > 0 then
            dVal.WorldBossIsFirstFresh = 1 
        end
	end
end

--[[
是否在场景内
]]
function IsInWorldBoss( sceneId )
    --print("IsInWorldBoss, sceneId="..sceneId)
    if sceneId == WorldBossCfg.sceneId then
        return true
    end
    return false
end

function GetWorldBossLastTime( )
	local dVal = System.getDyanmicVar()
    if dVal.WorldBossStartTime then
    	local now = System.getCurrMiniTime()    
	    local lastTime = dVal.WorldBossStartTime + WorldBossCfg.timer.activityTime - now + WorldBossCfg.timer.startDelayTime
	    if lastTime > 0 then
	        return lastTime
	    end
    end
    return 0
end

function OnWorldBossBosssMob( monster, monId, sceneId, x, y)
	--print("OnWorldBossBosssMob")
	ChangeWorldBossRightPanel( sysarg )
end
function OnWorldBossBosssDie( monster, sysarg, monId, lastHitKiller)
	--print("OnWorldBossBosssDie")
	ChangeWorldBossRightPanel( sysarg )
end

--[[填充面板内容信息]]
function FillWorldBossPanelContents( panelParams )
    if not panelParams.contents then
        panelParams.contents = {}
    end

    local nDays = System.getDayOfWeek()
	--print("FreshWorldBoss ... weekDay= "..nDays)
	if nDays == 0 then
		nDays = 7
	end
	local bossId = WorldBossCfg.bossId[nDays]
	if not bossId then
		return
	end 
	local dVal = System.getDyanmicVar()
	local stat = ""
	local monsterName = System.getMonsterNameById(bossId)
	local num = System.getMonsterCountById( bossId, WorldBossCfg.sceneId, 0 )
	if num > 0 then
		stat = Lang.ScriptTips.panelCont43   --已刷出
	elseif not dVal.WorldBossIsFirstFresh then
		stat = Lang.ScriptTips.panelCont45   --未刷出
	else 
		stat = Lang.ScriptTips.panelCont44   --已击杀
	end
	local info = string.format( Lang.ScriptTips.panelCont42, monsterName, stat )   --[XXXboss:XXXstat]
    info = string.format(Lang.ScriptTips.panelCont48, info)
    table.insert( panelParams.contents, info )
end

--[[
初始化右侧面板
]]
function InitWorldBossRightPanel( sysarg )
    local sceneId = Actor.getSceneId( sysarg )
    if not IsInWorldBoss( sceneId ) then           
        return
    end

    local activityType      = 2             --活动
    WorldBossCfg.restTime = GetWorldBossLastTime()
   
    local panelParams       = {}
    panelParams.sts             = 1                 --进行中
    panelParams.contents        = {}
    FillWorldBossPanelContents( panelParams)

    panelParams.buttons = {1}   --1：退出副本，2-单倍领奖，3-双倍领奖，4-查看排行榜，5-押镖寻路, 6-打开答题面板，7-放弃押镖，8-寻找活动NPC

    OpenCommonStylePanel( sysarg, activityType, enRightPanelActivity_WorldBoss, nil, Lang.ScriptTips.WorldBossName, WorldBossCfg, 8, buttons, panelParams )
end

function ChangeWorldBossRightPanel( sysarg )
	local panelParams       = {}
    panelParams.contents    = {}

    FillWorldBossPanelContents( panelParams)

    local buttons1      = {1}
    local buttons2      = {1}

    --广播给场景内所有的玩家
    local playerList = LuaHelp.getSceneActorListById( WorldBossCfg.sceneId ) or {}
    for i,player in ipairs( playerList ) do
        ChangeCommonStylePanel( player, 1, 8, WorldBossCfg, buttons1, buttons2, panelParams )
    end
  
end

--[[
活动结束
]]
function WorldBossStop(  )
    System.broadcastTipmsg( Lang.ScriptTips.WorldBoss001, ttScreenCenter + ttChatWindow )  --广播活动结束
    WorldBossPlayerOut()
    ClearWorldBossAllMonsters()
    local dVal = System.getDyanmicVar()
    dVal.WorldBossIsFirstFresh = nil 

    local NpcPtr = System.getGlobalNpc()
    Actor.unregScriptCallback(NpcPtr, "WorldBossStop")
    Actor.unregScriptCallback(NpcPtr, "FreshWorldBoss")
end

--踢出活动玩家
function WorldBossPlayerOut()
    local playerList = LuaHelp.getSceneActorListById( WorldBossCfg.sceneId ) or {}
    for i, player in ipairs( playerList or {} ) do
        Actor.returnCity( player )
    end
end

function ClearWorldBossAllMonsters()
    local nDays = System.getDayOfWeek()
    if nDays == 0 then
        nDays = 7
    end
    local bossId = WorldBossCfg.bossId[nDays]
    if bossId then
        local hScene = Fuben.getSceneHandleById(WorldBossCfg.sceneId, 0)
        if hScene then
            Fuben.clearMonster(hScene,  bossId)
        end
    end
end

SceneExitDispatcher.register( WorldBossCfg.sceneId,  OnCommonSceneExit )				--注册离开场景、副本后触发的事件